package com.levi.juc.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shentong
 * @since 2022/6/13 10:11 PM
 */
public class Test {
    public static void main(String[] args) {
//        Data2 data2 = new Data2();
//        for (int i = 0; i < 8; i++) {
//            new Thread(data2::incr,"A").start();
//            new Thread(data2::decre,"B").start();
//        }

        Data3 data3 = new Data3();
        for (int i = 0; i < 4; i++) {
            new Thread(data3::printA,"A").start();
            new Thread(data3::printB,"B").start();
            new Thread(data3::printC,"C").start();
        }
    }



}


class Data2{
    private int num = 0;

    private final Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void incr(){
        lock.lock();
        try {
            while (num != 0){
                condition.await();
            }
            num++;
            System.out.println(Thread.currentThread().getName() + "加1后的num" + num);
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decre(){
        lock.lock();
        try {
            while (num != 1){
                condition.await();
            }
            num--;
            System.out.println(Thread.currentThread().getName() + "减1后的num" + num);
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}


class Data3{
    private int num = 0;
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void printA() {
        lock.lock();
        try {
            while (num != 0) {
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName() + " num : =>>" + num);
            num  = 1;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }


    public void printB() {
        lock.lock();
        try {
            while (num != 1) {
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName() + " num : =>>" + num);
            num = 2;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }


    public void printC() {
        lock.lock();
        try {
            while (num != 2) {
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName() + " num : =>>" + num);
            num = 0;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

}